package cn.iocoder.yudao.module.system.controller.app.partnerapply;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.partnerapply.vo.PartnerApplyPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.partnerapply.vo.PartnerApplyRespVO;
import cn.iocoder.yudao.module.system.controller.admin.partnerapply.vo.PartnerApplySaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.partnerapply.PartnerApplyDO;
import cn.iocoder.yudao.module.system.service.partnerapply.PartnerApplyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 合伙人申请")
@RestController
@RequestMapping("/system/partnerapply")
@Validated
public class AppPartnerApplyController {

    @Resource
    private PartnerApplyService partnerApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建合伙人申请")
    public CommonResult<Long> createPartnerApply(@Valid @RequestBody PartnerApplySaveReqVO createReqVO) {
        return success(partnerApplyService.createPartnerApply(getLoginUserId(), createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合伙人申请")
    public CommonResult<Boolean> updatePartnerApply(@Valid @RequestBody PartnerApplySaveReqVO updateReqVO) {
        partnerApplyService.updatePartnerApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合伙人申请")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deletePartnerApply(@RequestParam("id") Long id) {
        partnerApplyService.deletePartnerApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合伙人申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<PartnerApplyRespVO> getPartnerApply(@RequestParam("id") Long id) {
        PartnerApplyDO partnerApply = partnerApplyService.getPartnerApply(id);
        return success(BeanUtils.toBean(partnerApply, PartnerApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合伙人申请分页")
    public CommonResult<PageResult<PartnerApplyRespVO>> getPartnerApplyPage(@Valid PartnerApplyPageReqVO pageReqVO) {
        PageResult<PartnerApplyDO> pageResult = partnerApplyService.getPartnerApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PartnerApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合伙人申请 Excel")
    @OperateLog(type = EXPORT)
    public void exportPartnerApplyExcel(@Valid PartnerApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PartnerApplyDO> list = partnerApplyService.getPartnerApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "合伙人申请.xls", "数据", PartnerApplyRespVO.class,
                        BeanUtils.toBean(list, PartnerApplyRespVO.class));
    }

}